﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class ContactController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IContactRepository contactRepository;
        public ContactController(ICommandBus commandBus, IContactRepository contactRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.contactRepository = contactRepository;
        }
        //
        // GET: /Contact/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = contactRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(ContactFormModels frm)
        {
            var form = new ContactFormModels()
            {
                ID = frm.ID,
                Code = frm.Code,
                ContactName = frm.ContactName,
                Description = frm.Description,
                //Email=Email,
                //Fax_Num=Fax_Num,
                PostAddress = frm.PostAddress,
                //PostAttention=PostAttention,
                //PostCity=PostCity,
                //PostCountry=PostCountry,
                Tel_Num = frm.Tel_Num,
                //Type = Type,
                Website = frm.Website
            };
            var command = Mapper.Map<ContactFormModels, CreateOrUpdateContactCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = contactRepository.GetAll();
            return PartialView("_ContactList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public JsonResult Get(int ID)
        {
            Contact product = contactRepository.GetById(ID);
            var obj = Mapper.Map<Contact, ContactFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteContactCommand> list = new List<DeleteContactCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteContactCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = contactRepository.GetAll();
            return PartialView("_ContactList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Contact> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = contactRepository.GetAll();
            }
            else
            {
                model = contactRepository.GetMany(x => (x.ContactName.Contains(Name)) || (x.Description.Contains(Name)) || (x.Description.Contains(Name)));

            }
            return PartialView("_ContactList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = contactRepository.GetAll();
            return PartialView("_ContactList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
